<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        div {
            height: 100px;
            width: 100px;
            margin: 10px;
        }
        td {
            margin: 10px;
            width: 30px;
            height: 20px;
            text-align: center;
        }
    </style>
</head>
<body>
    <script>
        // 1.封装判断一个年份是闰年还是平年，入参是year
        function isLeapYear(year) {
            if (year % 4 == 0 && year % 100!= 0) {
                return true;
            } else if (year % 100 == 0 && year % 400 == 0) {
                return true;
            } else {
                return false;
            }
        }
        console.log(isLeapYear(2010) == true ? "闰年" : "平年")

        // // 2.封装判断工作日 or 周末，入参是day
        function week(day) {
            switch (day) {
                case 1:
                    document.write("今天是周一，工作日");
                    break;
                case 2:
                    document.write("今天是周二，工作日");
                    break;
                case 3:
                    document.write("今天是周三，工作日");
                    break;
                case 4:
                    document.writet("今天是周四，工作日");
                    break;
                case 5:
                    document.write("今天是周五，工作日");
                    break;
                case 6:
                    document.write("今天是周六，休息日");
                    break;
                case 7:
                    document.write("今天是周日，休息日");
                    break;
                default:
                    document.write("输入错误");
                    break;
            }
        }
        week(1);

        // 3.封装获取某个范围之间的所有质数
        function getPrimes(m, n) {
            let primes = [];
            for (let i = m; i <= n; i++) {
                for (let j = 2; j <= i; j++) {
                    if (i % j == 0 && i == j) {
                        primes.push(i);
                        break;
                    } else if (i % j == 0) {
                        break;
                    }
                }
            }
            return primes;
        }
        console.log(getPrimes(1, 100));

        // 4.随机颜色16进制值
        function sixColor() {
            let color = "#";
            for (let i = 0; i < 6; i++) {
                let num = Math.floor(Math.random() * 16).toString(16);
                color += num;
            }
            return color;
        }
        document.write(`<div style="background-color: ${sixColor()}";></div>`);

        // 5.随机颜色rgb
        function rgbColor() {
            function c() {
                return Math.floor(Math.random() * 256);
            }
            return `rgb(${c()}, ${c()}, ${c()})`;
        }
        document.write(`<div style="background-color: ${rgbColor()}";></div>`);

        // 6.求m-n的所有阶乘之和
        function factorial(num) {
            let j = 1;
            for (let i = 1; i <= num; i++) {
                j *= i;
            }
            return j;
        }
        function sumFactorial(m, n) {
            let sum = 0;
            for (let i = m; i <= n; i++) {
                sum += factorial(i);
            }
            return sum;
        }
        console.log(sumFactorial(1, 5));

        // 7.打印m组六位验证码
        function sixCode(m) { 
            for (let j = 0; j < m; j++) {
                let code = "";
                for (let i = 0; i < 6; i++) {
                    a = (parseInt(Math.random() * 10)).toString();
                    code += a;
                }
                document.write(`${code}</br>`);
            }
        }
            
        sixCode(3);

        // 8.打印m*n的表格，格子内放n的数
        function table(m, n) {
            document.write("<table cellspacing=0 border=1 bordercolor=black>");
            for (let i = 1; i <= m; i++) {
                document.write("<tr>");
                for (let j = 1; j <= n; j++) {
                    document.write(`<td>${j}</td>`);
                }
                document.write("</tr>");
            }
            document.write("</table>");
        }
        table(3, 4);

        // 9.求m,n的最大公约数、最小公倍数
        m = 12;
        n = 24;
        function gcd(m, n) {
            for (let i = m; i >= 1; i--){
                if (m % i == 0 && n % i == 0) {
                    return i;
                }
            }
        }
        function lcm(m, n) {
            return m * n / gcd(m, n);
        }
        document.write(`最大公因数：${gcd(m,n)} </br> 最小公倍数：${lcm(m,n)}`)

        // 10.外卖
        function distance(num) {
            num = Math.ceil(num);
            if (num <= 3) {
                return 5;
            } else if (num > 3) {
                return 5 + (num - 3) * 2;
            }
        }
        console.log(distance(5));

        // 11.加密
        function encryption(photoNumber) {
            let num1 = photoNumber.substring(3, 4) * 3 % 10
            let num2 = photoNumber.substring(4, 5) * 4 % 10
            let num3 = photoNumber.substring(5, 6) * 5 % 10
            let num4 = photoNumber.substring(6, 7) * 6 % 10
            let num5 = photoNumber.substring(7, 8) * 7 % 10
            let num6 = photoNumber.substring(8, 9) * 8 % 10
            let num7 = photoNumber.substring(9, 10) * 9 % 10
            let num8 = photoNumber.substring(10) ** 2 % 10

            return photoNumber.substring(0, 3) + num8 + num2 + num6 + num4 + num5 + num1 + num7 + num3
        }
        console.log(encryption("17622636822"))
    </script>
</body>
</html>